<template>
  <!-- 导航部分 -->
  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>仓储管理</el-breadcrumb-item>
        <el-breadcrumb-item>仓库设置</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
  </el-row>

  <!--搜索栏-->
  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-form inline>
        <el-form-item label="仓库编码">
          <el-input v-model="storeQuery.storeCode"/>
        </el-form-item>
        <el-form-item label="仓库名称">
          <el-input v-model="storeQuery.storeName"/>
        </el-form-item>
        <el-form-item>
          <el-button @click="loadStore" type="primary">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>

  <!-- 工具栏 -->
  <el-row style="margin: 20px">
    <el-col :span="24">
      <el-button type="primary" plain @click="toAdd">
        <el-icon>
          <Plus/>
        </el-icon>
        新增
      </el-button>
      <el-button type="success" plain @click="toUpdate">
        <el-icon>
          <EditPen/>
        </el-icon>
        编辑
      </el-button>
      <el-button type="warning" plain @click="toDeletes">
        <el-icon>
          <EditPen/>
        </el-icon>
        删除
      </el-button>
    </el-col>
  </el-row>

  <!-- 数据列表 -->
  <el-row style="margin: 0px 10px;padding: 0px 15px">
    <el-col :span="24">
      <el-table :data="stores" border stripe ref="storeTable">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="storeCode" label="仓库编码">
          <template #default="scope">
            <el-link @click="showDetail(scope.row.storeId)" type="success">
              {{ scope.row.storeCode }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column prop="storeName" label="设备名称"/>
        <el-table-column prop="storePosition" label="设备品牌"/>
        <el-table-column prop="storeAreas" label="规格型号"/>
        <el-table-column prop="storePersonName" label="所属车间"/>
        <el-table-column prop="storeNotes" label="设备状态"/>
        <el-table-column label="操作" width="330">
          <template #default="scope">
            <el-link style="margin: 0px 5px" @click="toEdit(scope.row.storeId)" type="primary">
              <el-icon>
                <EditPen/>
              </el-icon>
              编辑
            </el-link>
            <el-link style="margin: 0px 5px" @click="toDelete(scope.row.storeId)" type="primary">
              <el-icon>
                <Delete/>
              </el-icon>
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>

  <el-row style="margin-top: 10px">
    <!-- 监听页码的变化事件 -->
    <el-pagination @change="changePage" :total="total" background layout="prev, pager, next"/>
  </el-row>


  <!-- 新增的弹窗 -->
  <el-dialog v-model="addStoreSettingVisible"
             title="新增仓库配置"
             width="50%"
  >
    <el-form :model="newSetting">
      <el-form-item label="仓库编码">
        <el-input v-model="newSetting.storeCode"/>
      </el-form-item>
      <el-form-item label="仓库名称">
        <el-input v-model="newSetting.storeName"/>
      </el-form-item>
      <el-form-item label="位置">
        <el-input v-model="newSetting.storePosition"/>
      </el-form-item>
      <el-form-item label="面积">
        <el-input v-model="newSetting.storeAreas"/>
      </el-form-item>
      <el-form-item label="负责人">
        <el-input v-model="newSetting.storePersonName"/>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="newSetting.storeNotes"/>
      </el-form-item>

    </el-form>
    <el-button @click="addStoreSettingVisible =false" newSetting={}>取消</el-button>
    <el-button type="primary" @click="addSetting">
      确定
    </el-button>
  </el-dialog>

  <!--编辑子组件-->
  <SettingEdit :storeId="updateSettingStoreId" @editComplate="loadStore"/>

  <!--详情子组件-->
  <storeDetail :storeId="detailStoreId"/>

</template>

<script setup>

import {onMounted, ref} from "vue";
import {Delete, EditPen} from "@element-plus/icons-vue";
import storeRequest from "@/api/store.js";
import {ElMessage, ElMessageBox} from "element-plus";
import SettingEdit from "@/views/store/storeSettingEdit.vue";
import storeDetail from "@/views/store/storeSettingDetail.vue";

let storeQuery =ref({});
let stores = ref([]);
let curPage = ref(1);
let total = ref(0);
let addStoreSettingVisible = ref(false);
let newSetting = ref({});
let updateSettingStoreId = ref(-1);
let storeTable = ref([]);
let detailStoreId = ref(-1);

function showDetail(storeId) {
  detailStoreId.value = storeId;
}

//工具栏添加
function toAdd() {
  addStoreSettingVisible.value=true;
}

//工具栏添加提交按钮
function addSetting() {
  storeRequest.addSetting(newSetting.value,res=>{
    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "添加成功"
      })
    } else {
      ElMessage({
        type: "warning",
        message: "添加失败"
      })
    }
    newSetting.value = {};
    addStoreSettingVisible.value = false;
    loadStore();
  })
}

//工具栏编辑
function toUpdate() {
  let selectionRows = storeTable.value.getSelectionRows();
  if (selectionRows.length > 1){
    ElMessage({
      type:"warning",
      message:"只能选择一条数据"
    })
  }else if(selectionRows.length == 0){
    ElMessage({
      type:"warning",
      message:"请选择数据"
    })
  }else{
    toEdit(selectionRows[0].storeId);
  }
}

//工具栏删除
function toDeletes() {
  let selectionRows = storeTable.value.getSelectionRows();
  if (selectionRows.length == 0) {
    ElMessage({
      type: "warning",
      message: "请选择数据"
    })
    return;
  }
  ElMessageBox.confirm(
      '您确定要删除吗？',
      'warning',
      {
        confirmButtonClass: '是的',
        cancelButtonClass: '取消',
        type: 'warning',
      }
  ).then(() => {
    selectionRows.forEach(row => {
      storeRequest.delete(row.storeId, res => {
        if (res.code == 1) {
          ElMessage({
            type: "success",
            message: "删除成功",
          })
        } else {
          ElMessage({
            type: "warning",
            message: "删除失败",
          })
        }
      })
    })
    loadStore();
  })
}

//行编辑
function toEdit(storeId) {
  updateSettingStoreId.value=storeId;
}

//行删除
function toDelete(storeId) {
  ElMessageBox.confirm(
      '您确定要删除吗',
      'Warning',
      {
        confirmButtonClass: '是的',
        cancelButtonClass: '取消',
        type: 'warning',
      }
  ).then(() => {
   storeRequest.delete(storeId, res => {
      if (res.code == 1) {
        ElMessage({
          type: "success",
          message: "操作完成"

        })
        loadStore();
      } else {
        ElMessage({
          type: "warning",
          message: "删除失败"
        })
      }
    })
    loadStore();
  })
}

//加载设备
function loadStore() {
  storeRequest.loadSetting(storeQuery.value, curPage.value, 10, res => {
    total.value = res.data.total;
    stores.value = res.data.list;
  })
}

//分页
function changePage(value) {
  curPage.value = value;
  loadStore();
}

onMounted(() => {
  loadStore();
})
</script>

<style scoped>

</style>